package by.grsu.na.page.sample;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.na.app.MyWebApplication;
import by.grsu.na.dao.impl.ProductDao;
import by.grsu.na.entity.Product;

public class ProductEditPage extends WebPage {

	public ProductEditPage(final Product product) {
		super();


		final Form<Product> form = new Form<Product>("form", new CompoundPropertyModel<Product>(product));
		add(form);


		form.add(new TextField<Double>("cost"));
		form.add(new TextField<String>("type"));


		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				final ProductDao productDao = new ProductDao(MyWebApplication.DATA_XML_FOLDER);
				// is new Circle?
				if (product.getId() == null) {

					productDao.saveNew(product);
				} else {

					productDao.update(product);
				}

				setResponsePage(new ProductsListPage());
			}
		});

		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {

				setResponsePage(new ProductsListPage());
			}
		});
	}
}
